<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>引出生命周期</title>
</head>
<body>
    <!-- 引入React核心库 -->
    <script src="../JS/react.development.js"></script>
    <!-- 引入ReactDOM，用于支持react操作DOM -->
    <script src="../JS/react-dom.development.js"></script>
    <!-- 引入babel 用于将书写的JSX语法 编译成JS语法 -->
    <script src="../JS/babel.min.js"></script>

    <div id="test"></div>
    <script type="text/babel">
        class Life extends React.Component{
            state={
                opacity:0.5
            }

            // 卸载组件
            death = ()=>{
                ReactDOM.unmountComponentAtNode(document.getElementById('test'))
            }
            // 组件挂载在页面之后调用（组件挂载完毕）Vue中的mounted
            // 只调用一次
            componentDidMount(){
                this.timer = setInterval(()=>{
                    let {opacity} = this.state
                    opacity -= 0.1
                    if(opacity<=0) opacity = 1
                    this.setState({opacity})
                },200)
            }
            // 卸载组件前
            componentWillUnmount(){
                clearInterval(this.timer)
            }
            // 初始化渲染 ,状态更新后
            // 调用次数 1+n 次 初始化渲染 调用一次
            render(){
                return(
                    <div>
                        <h4 style={{opacity:this.state.opacity}}>React 学不会怎么办</h4>
                        <button onClick={this.death}>不活了</button>    
                    </div>
                )
            }
        }
        ReactDOM.render(<Life /> ,document.getElementById('test'))
    </script>


    
</body>
</html>